package com.banzhi.shop.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.banzhi.shop.R;
import com.banzhi.shop.activity.CommodityDetailsActivity;
import com.banzhi.shop.entity.OrderManagement.OrderInfo_data;
import com.banzhi.shop.entity.OrderManagement.Order_Data;
import com.banzhi.shop.utils.ASUtils;
import com.banzhi.shop.utils.DateUtil;
import com.banzhi.shop.utils.NumberUtils;

import java.util.List;

/**
 * Created by HT on 2017/5/10.
 */

public class CustomeOrderAdapter extends BaseAdapter<Order_Data> {
    private Context context;

    public CustomeOrderAdapter(Context context, List<Order_Data> data) {
        super(context, data);
        this.context = context;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final Order_Data data = getData().get(position);
        viewHolder holder = null;
        if (convertView == null) {
            holder = new viewHolder();
            convertView = getInflater().inflate(R.layout.item_custom_order, null);
            holder.tvtime = (TextView) convertView.findViewById(R.id.tv_time);
            holder.tvzongjia = (TextView) convertView.findViewById(R.id.tv_zongjia);
            holder.tvyunfei = (TextView) convertView.findViewById(R.id.tv_yunfei);
            holder.ivmai = (ImageView) convertView.findViewById(R.id.iv_mai);
            holder.tvstatus = (TextView) convertView.findViewById(R.id.tv_statu);

            holder.recyclerView = (RecyclerView) convertView.findViewById(R.id.recyclerview);
            LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
            layoutManager.setSmoothScrollbarEnabled(true);
            layoutManager.setAutoMeasureEnabled(true);
            layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            holder.recyclerView.setLayoutManager(layoutManager);
            holder.recyclerView.setHasFixedSize(true);
            holder.recyclerView.setNestedScrollingEnabled(false);
            convertView.setTag(holder);
        }
        holder = (viewHolder) convertView.getTag();
        if (ASUtils.isCanUse(data.getCreatetime())) {
            holder.tvtime.setText(DateUtil.getTimeZhiData(data.getCreatetime()));
        }
        if (ASUtils.isCanUse(data.getGoodsprice())) {
            holder.tvzongjia.setText(NumberUtils.DecimalFormat(data.getGoodsprice()));
        }
        if (ASUtils.isCanUse(data.getDispatchprice())) {
            holder.tvyunfei.setText(NumberUtils.DecimalFormat(data.getDispatchprice()));
        }
        if ("1".equals(data.getShoperOrder())) {
            holder.ivmai.setImageResource(R.mipmap.mai);
        } else {
            holder.ivmai.setImageResource(R.mipmap.mai1);
        }
        if ("0".equals(data.getStatus())) {
            holder.tvstatus.setText("待付款");
        } else if ("1".equals(data.getStatus())) {
            holder.tvstatus.setText("待发货");
        } else if ("2".equals(data.getStatus())) {
            holder.tvstatus.setText("待收货");
        } else if ("3".equals(data.getStatus())) {
            holder.tvstatus.setText("已完成");
        }


        CommonAdapter<OrderInfo_data> adapter = new CommonAdapter<OrderInfo_data>(context, R.layout.item_order_management_shangping, data.getOrderGoods()) {
            @Override
            public void convert(ViewHolder holder, OrderInfo_data orderInfodata, int position) {
                holder.setImageURI(R.id.iv_image, orderInfodata.getThumb());
                holder.setText(R.id.tv_title, orderInfodata.getTitle());
                if (orderInfodata.getOptionname() != null && !"".equals(orderInfodata.getOptionname())) {
                    holder.getTextView(R.id.tv_text_guige).setVisibility(View.VISIBLE);
                    holder.setText(R.id.tv_guige, orderInfodata.getOptionname());
                } else {
                    holder.getTextView(R.id.tv_text_guige).setVisibility(View.GONE);
                }
                if (orderInfodata.getPrice() != null && !"".equals(orderInfodata.getPrice())) {
                    Double d = Double.parseDouble(orderInfodata.getPrice()) / Double.parseDouble(orderInfodata.getTotal());
                    holder.setText(R.id.tv_danjia, NumberUtils.DecimalFormat(d + ""));
                }
                holder.setText(R.id.tv_num, orderInfodata.getTotal());
                if (orderInfodata.getCommission1() != null && !"".equals(orderInfodata.getCommission1())) {
                    holder.setText(R.id.tv_commission, NumberUtils.DecimalFormat(orderInfodata.getCommission1()));
                }
                if ("1".equals(data.getShoperOrder())) {
                    holder.getImageView(R.id.iv_shen_zhuan).setImageResource(R.mipmap.sheng);
                } else {
                    holder.getImageView(R.id.iv_shen_zhuan).setImageResource(R.mipmap.zhuan);
                }

            }
        };
        holder.recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new CommonAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(getContext(), CommodityDetailsActivity.class);
                intent.putExtra("id", data.getOrderGoods().get(position).getGoodsid());
                getContext().startActivity(intent);
            }
        });
        return convertView;
    }

    class viewHolder {
        TextView tvtime;
        ImageView ivmai;
        TextView tvzongjia;
        TextView tvyunfei;
        TextView tvstatus;
        RecyclerView recyclerView;
    }


}